package com.photography.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.annotation.RequireRole;
import com.photography.common.Result;
import com.photography.entity.Order;
import com.photography.service.OrdersService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-05-23
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Resource
    private OrdersService ordersService;

    /**
     * 获取最近30天的每日订单统计
     * @return 每日订单统计结果
     */
    @GetMapping("/trends")
    public Result getDailyOrderStats() {
        List<Map<String, Object>> stats = ordersService.getDailyOrderCount();
        return Result.success(stats, "获取每日订单统计成功");
    }

    @GetMapping("/top")
    public Result getTopProduct() {
        List<Map<String, Object>> stats = ordersService.getTopProductCount();
        return Result.success(stats, "获取销售统计成功");
    }

    /**
     * 获取最近30天产品分类销售分布
     * @return 分类销售分布统计结果
     */
    @GetMapping("/category")
    public Result getCategoryDistribution() {
        List<Map<String, Object>> stats = ordersService.getCategoryDistribution();
        return Result.success(stats, "获取分类销售分布成功");
    }

    /**
     * 创建订单
     *
     * @param order 订单信息
     * @return 创建结果
     */
    @PostMapping("/create")
    public Result createOrder(@RequestBody Order order) {
        return ordersService.createOrder(order);
    }

    /**
     * 更新订单状态
     *
     * @param orderId 订单ID
     * @param orderStatus 订单状态
     * @return 更新结果
     */
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer orderId, @RequestParam String orderStatus) {
        return ordersService.updateStatus(orderId, orderStatus);
    }

    /**
     * 更新所有
     */
    @PutMapping("/updateAll")
    public Result updateAll(@RequestBody Order order){
        return ordersService.updateAll(order);
    }

    /**
     * 删除订单
     *
     * @param orderId 订单ID
     * @return 删除结果
     */
    @DeleteMapping("/{orderId}")
    public Result deleteOrder(@PathVariable Integer orderId) {
        return ordersService.deleteOrder(orderId);
    }

    /**
     * 分页查询订单列表
     *
     * @param current 当前页码
     * @param size 每页大小
     * @return 分页结果
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer current,
                      @RequestParam(defaultValue = "10") Integer size,String orderStatus,Integer orderId) {
        System.out.println(size);
        Page<Order> page = new Page<>(current, size);
        Page<Order> orderPage = ordersService.getOrdertPage(page,orderStatus,orderId);
        return Result.success(orderPage, "查询成功");
    }

    /**
     * 根据ID查询订单详情
     *
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("/{orderId}")
    public Result findById(@PathVariable Integer orderId) {
        return ordersService.findById(orderId);
    }

    @GetMapping("/status/{userId}")
    public Result findOrderByStatus(@PathVariable Integer userId){
        return ordersService.findOrderByStatus(userId);
    }

    /**
     * 获取今日统计数据
     * @return 今日统计数据
     */
    @GetMapping("/today")
    public Result getTodayStatistics() {
        List<Map<String, Object>> stats = ordersService.getTodayStatistics();
        return Result.success(stats, "获取今日统计数据成功");
    }

}
